#include "kdpch.h"
#include "OpenGLRendererAPI.h"

#include <glad/glad.h>

namespace KD {

	void OpenGLRendererAPI::Init()
	{
		glad_glEnable(GL_BLEND); // 启用混合
		glad_glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // 设置混合函数

		glad_glEnable(GL_DEPTH_TEST); // 启用尝试测试
	}

	void OpenGLRendererAPI::SetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height)
	{
		glViewport(x, y, width, height);
	}

	void OpenGLRendererAPI::SetClearColor(const glm::vec4& color)
	{
		glad_glClearColor(color.r, color.g, color.b, color.a);
	}

	void OpenGLRendererAPI::Clear()
	{
		glad_glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	}

	void OpenGLRendererAPI::DrawIndexed(const std::shared_ptr<VertexArray>& vertexArray, const uint32_t indexCount)
	{
		uint32_t count = indexCount == 0 ? vertexArray->GetIndexBuffers()->GetCount() : indexCount;
		glad_glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr);
		glad_glBindTexture(GL_TEXTURE_2D, 0);
	}

}